package com.lsy.weizhi2.activity;

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.fragment.LoginFragment;
import com.lsy.weizhi2.fragment.RegisterFragment;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.myutils.FragmentCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.StatusCode;


public class LoginActivty extends MyBaseActivity implements View.OnClickListener,FragmentCall {
    private TextView login_login_tv,login_register_tv,login_forget_password,login_agreement_tv;
    private View login_login_line,login_register_line,login_forget_line,back_view;
    private FrameLayout frame_fragment;
    private LinearLayout login_agreement;
    private CheckBox login_agree_check;
    private LoginFragment loginFragment;
    private RegisterFragment registerFragment;
    private android.app.FragmentManager manager;
    private FragmentTransaction ft;
    private Context context;
    private boolean agree;
    private int xx,height;
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_activty);
        Init();
        Log.i("message","onCreate");
    }

    private void Init() {
        context=LoginActivty.this;
        back_view=findViewById(R.id.back_view);
        InitWidget();
        InitLayout();
    }
    private void InitWidget() {
        login_login_tv= (TextView) findViewById(R.id.login_login_tv);
        login_register_tv= (TextView) findViewById(R.id.login_register_tv);
        login_forget_password= (TextView) findViewById(R.id.login_forget_password);
        login_login_line=findViewById(R.id.login_login_line);
        login_register_line=findViewById(R.id.login_register_line);
        login_forget_line=findViewById(R.id.login_forget_line);
        login_agree_check= (CheckBox) findViewById(R.id.login_agree_check);
        login_agreement_tv= (TextView) findViewById(R.id.login_agreement_tv);
        login_login_tv.setOnClickListener(this);
        login_register_tv.setOnClickListener(this);
        login_forget_password.setOnClickListener(this);
        login_agreement_tv.setOnClickListener(this);
        login_agree_check.setOnClickListener(this);

    }
    private void InitLayout(){
        login_agreement= (LinearLayout) findViewById(R.id.login_agreement);
        frame_fragment= (FrameLayout) findViewById(R.id.frame_fragment);
        loginFragment=new LoginFragment();
        registerFragment=new RegisterFragment();
        manager=getFragmentManager();
        ft=manager.beginTransaction();
        ft.add(R.id.frame_fragment,loginFragment);
        ft.add(R.id.frame_fragment,registerFragment);
        ft.commit();
        Select_Login();
    }
    private void Select_Login(){
        login_login_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
        login_login_line.setVisibility(View.VISIBLE);
        login_register_tv.setTextColor(getResources().getColor(R.color.clicked_no));
        login_register_line.setVisibility(View.GONE);
        login_forget_password.setVisibility(View.VISIBLE);
        login_agreement.setVisibility(View.GONE);
        show_Login();
    }
    private void Select_Register(){
        login_login_tv.setTextColor(getResources().getColor(R.color.clicked_no));
        login_login_line.setVisibility(View.GONE);
        login_register_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
        login_register_line.setVisibility(View.VISIBLE);
        login_forget_password.setVisibility(View.GONE);
        login_agreement.setVisibility(View.VISIBLE);
        show_Register();
    }
    private void show_Login(){
        ft=manager.beginTransaction();
        ft.replace(R.id.frame_fragment,loginFragment);
        ft.commit();
    }
    private void show_Register(){
        ft=manager.beginTransaction();
        ft.replace(R.id.frame_fragment,registerFragment);
        ft.commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ViewTreeObserver vto = back_view.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        height = back_view.getHeight();
                        if (xx < 1) {
                            xx++;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(back_view.getLayoutParams());
                                    params.height = MyHeight.ACTIONBAR + height;
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                        back_view.setLayoutParams(params);
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }).start();
        if(StatusCode.AGREEMENT){
            login_agree_check.setChecked(true);
            registerFragment.setCheck(true);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_login_tv:
                Select_Login();
                break;
            case R.id.login_register_tv:
                Select_Register();
                break;
            case R.id.login_forget_password:
                MyMethod.toActivity(context,Forget_Password.class);
                break;
            case R.id.login_agreement_tv://用户协议书
                MyMethod.toActivity(context,Agreement.class);
                break;
            case R.id.login_agree_check://确认同意
                agree=login_agree_check.isChecked();
                registerFragment.setCheck(agree);
                break;
        }
    }

    @Override
    public void Select(int flag) {
        switch (flag){
            case 1:
                Select_Register();
                break;
            case 0:
                Select_Login();
                break;
        }

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
            finish();
            System.exit(0);
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
}
